package com.sky.service.impl;

import com.sky.context.ThreadLocalUtil;
import com.sky.entity.AddressBook;
import com.sky.entity.User;
import com.sky.mapper.AddressBookMapper;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
class AddressBookServiceImpl implements AddressBookService {

    @Autowired
    AddressBookMapper addressBookMapper;
    /**
     * 个人中心里的地址管理（查询登录人地址管理）
     * @return
     */
    @Override
    public List<AddressBook> list() {
        //获取当前用户id
        Long uid = ThreadLocalUtil.getCurrentId();
        List<AddressBook> addressBook=addressBookMapper.list(uid);
        return addressBook;
    }

    /**
     * 新增收货地址
     * @param addressBook
     */
    @Override
    public void add(AddressBook addressBook) {
        //补全数据库与请求体里的参数
         addressBook.setUserId(ThreadLocalUtil.getCurrentId());
        //调用mapper进行新增地址操作
        addressBookMapper.insert(addressBook);

    }

    /**
     * 设置默认收货地址
     * @param addressBook
     */
    @Override
    public void setDefaultAddress(AddressBook addressBook) {
        //将当前用户的收货地址默认为非默认
        addressBookMapper.allNotDefault(ThreadLocalUtil.getCurrentId());
        //更新当前id为默认
        addressBookMapper.getUpdateDefault(addressBook);
    }

    /**
     * 查询默认收货地址
     * @return
     */
    @Override
    public AddressBook getDefault() {
        //获取当前登录人的userId
        //根据userId查询默认收货地址
        AddressBook addressBook=addressBookMapper.getDefault(ThreadLocalUtil.getCurrentId());
        return addressBook;
    }


    /**
     * 修改地址簿
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        return addressBookMapper.getById(id);
    }



    /**
     * 保存地址
     * @return
     */
    @Override
    public void updateById(AddressBook addressBook) {
        addressBookMapper.updateById(addressBook);

    }
}
